package com.imooc.imoocbilibili.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.imooc.imoocbilibili.entity.FollowingGroup;
import com.imooc.imoocbilibili.entity.User;
import com.imooc.imoocbilibili.entity.UserFollowing;
import com.imooc.imoocbilibili.interceptor.ThreadUser;
import com.imooc.imoocbilibili.service.FollowingGroupService;
import com.imooc.imoocbilibili.service.UserFollowingService;
import com.imooc.imoocbilibili.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * 用户关注表
 *
 * @author jl
 * @since 2022-04-10 21:13:43
 */
@RestController
@RequiredArgsConstructor
public class UserFollowingController {

    private final UserFollowingService userFollowingService;
    private final FollowingGroupService followingGroupService;

    /**
     * 关注用户
     */
    @PostMapping("/user-followings")
    public R addUserFollowings(@RequestBody UserFollowing userFollowing) {
        User user = ThreadUser.getUser();
        Long userId = user.getId();
        userFollowing.setUserid(userId);
        userFollowingService.addUserFollowings(userFollowing);
        return R.ok();
    }

    /**
     * 获取用户关注列表
     */
    @GetMapping("/user-followings")
    public R getUserFollowings() {
        return R.ok(userFollowingService.getUserFollowings());
    }

    /**
     * 获取用户粉丝列表
     */
    @GetMapping("/user-fans")
    public R getUserFans() {
        return R.ok(userFollowingService.getUserFans());
    }

    /**
     * 新增用户自定义关注分组
     */
    @PostMapping("/user-following-groups")
    public R addUserFollowingGroups(@RequestBody FollowingGroup followingGroup) {
        User user = ThreadUser.getUser();
        Long userId = user.getId();
        followingGroup.setUserid(userId);
        followingGroup.setCreatetime(new Date());
        followingGroupService.save(followingGroup);
        return R.ok(followingGroup.getId());
    }

    /**
     * 获取用户的关注分组
     */
    @GetMapping("/user-following-groups")
    public R getUserFollowingGroups() {
        User user = ThreadUser.getUser();
        Long userId = user.getId();
        return R.ok(followingGroupService.list(new LambdaQueryWrapper<FollowingGroup>().eq(FollowingGroup::getUserid, userId)));
    }
}
